//
//  CellForHome.m
//  echo
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 Danny. All rights reserved.
//

#import "CellForHome.h"
#import "ModelForHome.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ModelForHome.h"
#import <DKNightVersion.h>
#import "NetworkingHandler.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

@interface CellForHome ()

@end

@implementation CellForHome

-(void)dealloc{
    [_viewOfBackGray release];
    [_viewOfBackWhite release];
    [_labelOfHeader release];
    [_labelOfBottom release];
    [_viewForEcho release];
    [_line release];
    [_button release];
    [_tempModel release];
    [_imageViewForPic release];
    [_labelForMusic release];
    [_buttonForComment release];
    [_buttonForPraise release];
    [_buttonForTransmit release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.viewOfBackGray = [[UIView alloc]init];
        self.viewOfBackGray.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.contentView addSubview:self.viewOfBackGray];
        [self.viewOfBackGray release];
        
        self.viewOfBackWhite = [[UIView alloc]init];
        self.viewOfBackWhite.backgroundColor = [UIColor whiteColor];
        [self.viewOfBackGray addSubview:self.viewOfBackWhite];
        [self.viewOfBackWhite release];
        
        self.labelOfHeader = [[UILabel alloc]init];
        [self.viewOfBackWhite addSubview:self.labelOfHeader];
        [self.labelOfHeader release];
        
        self.viewForEcho = [[UIImageView alloc]init];
        [self.viewOfBackWhite addSubview:self.viewForEcho];
        [self.viewForEcho release];
        
        self.line = [[UIView alloc]init];
        [self.viewOfBackWhite addSubview:self.line];
        [self.line release];
        
        
        self.labelOfBottom = [[UILabel alloc]init];
        [self.viewOfBackWhite addSubview:self.labelOfBottom];
        [self.labelOfBottom release];
        
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.viewOfBackWhite addSubview:self.button];
        
        self.imageViewForPic = [[UIImageView alloc]init];
        [self.viewOfBackWhite addSubview:self.imageViewForPic];
        [self.imageViewForPic release];
        
        self.labelForMusic = [[UILabel alloc]init];
        self.labelForMusic.backgroundColor = [UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1];
        [self.button addSubview:self.labelForMusic];
        [self.labelForMusic release];
        
        self.buttonForComment = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBackWhite addSubview:self.buttonForComment];
        [self.buttonForComment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.buttonForComment dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
        
        self.buttonForComment.layer.borderColor = [UIColor grayColor].CGColor;
        self.buttonForComment.layer.borderWidth = 0.7;
        
        self.buttonForPraise = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.viewOfBackWhite addSubview:self.buttonForPraise];
        [self.buttonForPraise setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.buttonForPraise.layer.borderColor = [UIColor grayColor].CGColor;
        self.buttonForPraise.layer.borderWidth = 0.7;
        [self.buttonForPraise dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
        self.buttonForTransmit = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.viewOfBackWhite addSubview:self.buttonForTransmit];
        [self.buttonForTransmit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.buttonForTransmit.layer.borderColor = [UIColor grayColor].CGColor;
        self.buttonForTransmit.layer.borderWidth = 0.7;
        [self.buttonForTransmit dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];

        [self handleNightModel];
    }
    return self;
}

- (void)handleNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.viewOfBackGray.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.viewOfBackWhite.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
     self.button.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.labelForMusic.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.labelOfHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelOfBottom.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForMusic.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);

}

- (void)passModel:(ModelForHome *)model{
    self.labelOfBottom.text = model.content;
    self.labelOfHeader.text = model.nameForTop;
    self.tempModel = [[ModelForHome alloc]init];
    self.tempModel = model;
    if (model.id) {
        NSString *str = @"http://echosystem.kibey.com/sound/info?sound_id=";
        NSString *url = [str stringByAppendingString:model.id];
        [NetworkingHandler networkingGetWithUrl:url dicForValue:@{@"x-v":@"20160209",@"x-c":@"3",@"Cookie":@"PHPSESSID=6a8frc7tqd9qtegbih7vlck5n0"} completeHandle:^(id result) {
            NSDictionary *dic = [result objectForKey:@"result"];
            NSString *strForPic = [dic objectForKey:@"pic_1080"];
            self.tempModel.pic_1080 = strForPic;
        }];
    }
}

+ (CGFloat)heightForBottomLabel:(NSString *)text{

    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 85 * WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"ArialUnicodeMS" size:17.1]} context:nil];

    return rect.size.height;
}

- (CGFloat)heightForCell:(ModelForHome *)model{
    
    CGFloat height = [CellForHome heightForBottomLabel:model.content];

    if (model.name != nil) {
       
        return height + (55 + 10 + 5 + 60 + 40 + 5) * HEIGHT;
    }else{
      
        return height + (55 + 10 + 40 + 10) * HEIGHT;
    }
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];

    self.viewOfBackGray.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.viewOfBackWhite.frame = CGRectMake(5 * WIDTH, 5 * HEIGHT, self.frame.size.width - 10 * WIDTH, self.frame.size.height - 10 * HEIGHT);
    
    self.labelOfHeader.frame = CGRectMake(70 * WIDTH, 5 * HEIGHT, self.frame.size.width - 85 * WIDTH, 40 * HEIGHT);
//    self.labelOfHeader.text = @"        echo回声";
    self.labelOfHeader.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];

    self.viewForEcho.frame = CGRectMake(15 * WIDTH, 10 * HEIGHT, 40 * WIDTH, 40 * WIDTH);
//    [self.viewForEcho sd_setImageWithURL:[NSURL URLWithString:@"http://kibey-sys-avatar.b0.upaiyun.com/2014/9/1410967526.png!150"]];
    [self.viewForEcho sd_setImageWithURL:[NSURL URLWithString:self.tempModel.avatar]];
//    [self.viewForEcho setImage:[UIImage imageNamed:@"http://kibey-sys-avatar.b0.upaiyun.com/2014/9/1410967526.png!150"]];
    self.viewForEcho.layer.cornerRadius = 20 * WIDTH;
    self.viewForEcho.layer.masksToBounds = YES;
    
    self.line.frame = CGRectMake(70 * WIDTH, 47 * HEIGHT, self.frame.size.width - 85 * WIDTH, 1 * HEIGHT);
    self.line.dk_backgroundColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);

    self.labelOfBottom.frame = CGRectMake(70 * WIDTH, 50 * HEIGHT, self.frame.size.width - 85 * WIDTH, [[self class] heightForBottomLabel:self.labelOfBottom.text]);
    self.labelOfBottom.numberOfLines = 0;
    self.labelOfBottom.textAlignment = NSTextAlignmentLeft;
    self.labelOfBottom.highlighted =YES;
    
    if ([self.tempModel.name class] != nil) {

        self.button.frame = CGRectMake(70 * WIDTH, 55 * HEIGHT + self.labelOfBottom.frame.size.height, self.frame.size.width - 85 * WIDTH, 60 * HEIGHT);

        [self.button addTarget:self action:@selector(handleButtonForMusic) forControlEvents:UIControlEventTouchUpInside];
        
        self.imageViewForPic.frame = CGRectMake(0, 0, 60 * WIDTH, 60 * WIDTH);
        [self.button addSubview:self.imageViewForPic];
        [self.imageViewForPic sd_setImageWithURL:[NSURL URLWithString:self.tempModel.pic]];
   
        self.labelForMusic.frame = CGRectMake(63 * WIDTH, 3 * HEIGHT, self.button.frame.size.width - 60 * WIDTH - 6 * WIDTH, 54 * HEIGHT);
        self.labelForMusic.text = self.tempModel.name;
        self.labelForMusic.lineBreakMode = 0;

        self.labelForMusic.font = [UIFont fontWithName:@"Arial" size:15.0];
        self.labelForMusic.textColor = [UIColor greenColor];
        self.labelForMusic.textAlignment = NSTextAlignmentCenter;
        self.labelForMusic.highlighted = YES;
        
    } else if ([self.tempModel.name class] == nil){

        self.button.hidden = YES;
    }
    
    if ([self.tempModel.name class] != nil) {
        
        self.buttonForComment.frame = CGRectMake(0, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10 + 60) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForComment setTitle:@"评论" forState:UIControlStateNormal];
        self.buttonForComment.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
        
        self.buttonForPraise.frame = CGRectMake(self.viewOfBackWhite.frame.size.width / 3, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10 + 60) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForPraise setTitle:@"赞" forState:UIControlStateNormal];
        self.buttonForPraise.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
        
        self.buttonForTransmit.frame = CGRectMake(self.viewOfBackWhite.frame.size.width / 3 * 2, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10 + 60) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForTransmit setTitle:@"转播" forState:UIControlStateNormal];
        self.buttonForTransmit.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
        
    }else if ([self.tempModel.name class] == nil){
        
        self.buttonForComment.frame = CGRectMake(0, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForComment setTitle:@"评论" forState:UIControlStateNormal];
        self.buttonForComment.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
        
        self.buttonForPraise.frame = CGRectMake(self.viewOfBackWhite.frame.size.width / 3, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForPraise setTitle:@"赞" forState:UIControlStateNormal];
        self.buttonForPraise.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
        
        self.buttonForTransmit.frame = CGRectMake(self.viewOfBackWhite.frame.size.width / 3 * 2, [[self class] heightForBottomLabel:self.labelOfBottom.text] + (55 + 10) * HEIGHT, self.viewOfBackWhite.frame.size.width / 3, 40 * HEIGHT);
        [self.buttonForTransmit setTitle:@"转播" forState:UIControlStateNormal];
        self.buttonForTransmit.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:17.0];
    }
}

- (void)handleButtonForMusic{
        
    ModelForHome *model = [[ModelForHome alloc]init];
    model = self.tempModel;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postModelForHome" object:nil userInfo:@{@"model":model}];
    [model release];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
